package com.youlai.boot.system.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.Formatter;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class DateTimeFormatterConverter implements Formatter<LocalDateTime> {
    @Autowired
    private final DateTimeFormatter  dateTimeFormatter;

    public DateTimeFormatterConverter(DateTimeFormatter dateTimeFormatter) {
        this.dateTimeFormatter = dateTimeFormatter;
    }

    @Override
    public LocalDateTime parse(String text, Locale locale) throws ParseException {
        return LocalDateTime.parse(text, dateTimeFormatter);
    }

    @Override
    public String print(LocalDateTime object, Locale locale) {
        return dateTimeFormatter.format(object);
    }
}
